# -*- coding: utf-8 -*-

import pytest
from system.DBManage import MysqlDB
from system.Config import *


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    out = yield  # 是通过yield关键字做轮循
    mysql = MysqlDB(host=host, user=user, password=password, database=database, port=port)

    report = out.get_result()
    if report.when == "call":  # 固定写法（固定过滤语句）
        # 下面的if 是获取我们 info 标签中注册的内容：比如我在用例中注册的是 team principal
        if "pytestmark" in item.function.__dict__:
            for i in item.function.pytestmark:
                if "info" in i.__dict__.values():
                    values = (report.location[0], report.location[-1], item.originalname, report.outcome,
                              i.__dict__["kwargs"]["team"], i.__dict__["kwargs"]["principal"])
                    mysql.change(
                        f"""insert into test_cases_1 (caseId, testId, test_name, result,team,principal) values {values};"""
                    )
                else:
                    values = (report.location[0], report.location[-1], item.originalname, report.outcome)
                    mysql.change(
                        f"""insert into test_cases_1 (caseId, testId, test_name, result) values {values};"""
                    )
        else:

            values = (report.location[0], report.location[-1], item.originalname, report.outcome)
            mysql.change(
                f"""insert into test_cases_1 (caseId, testId, test_name, result) values {values};"""
            )

        # print("测试结果:", report.outcome)  # 就是测试结果 非passed/skip  就是异常的用例
